package vn.cuhanh;

import vn.cuhanh.bean.ShareInfo;
import vn.cuhanh.fragment.MenuFragment;
import vn.cuhanh.utils.HelperFunc;
import vn.cuhanh.utils.ShareZingMe;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;

public class NewsDetailActivity extends BaseActivity {
	public static final int TYPE_URL = 1;
	public static final int TYPE_ASSET = 2;
	public static final int TYPE_FROM_DISK = 3;
	public static final int TYPE_RAW_CONTENT = 4;
	private int type = 1;
	private String content;
	private boolean haveShareBtn;
	private Button btnShare;
	public static ShareInfo shareInfo;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.news_detail);
		setBehindContentView(R.layout.menu_frame);
		MenuFragment menuFragment = MenuFragment.getInstance(this);
		getSupportFragmentManager().beginTransaction().replace(R.id.menu_frame, menuFragment).commit();
		
		Intent intent = getIntent();
		type = intent.getExtras().getInt("type");
		content = intent.getExtras().getString("content");
		if(intent.getExtras().containsKey("haveShareBtn")) {
			haveShareBtn = intent.getExtras().getBoolean("haveShareBtn");
		} else {
			haveShareBtn = false;
		}
		if(haveShareBtn) {
			initShareSection();
		}
		
		loadData();
	}
	
	public void loadData() {
		WebView webView = (WebView)findViewById(R.id.news_detail_content);
		switch (type) {
		case TYPE_URL:
			webView.loadUrl(content);
			webView.getSettings().setLoadWithOverviewMode(true);
			webView.getSettings().setUseWideViewPort(true);
			webView.getSettings().setBuiltInZoomControls(true);
	        webView.getSettings().setSupportZoom(true); 
			break;
		case TYPE_ASSET:
			webView.loadUrl("file:///android_asset/" + content);
			webView.getSettings().setLoadWithOverviewMode(true);
			webView.getSettings().setUseWideViewPort(true);
			webView.getSettings().setBuiltInZoomControls(true);
	        webView.getSettings().setSupportZoom(true); 
//			webView.setInitialScale(30);
//			webView.setP
			break;
		case TYPE_RAW_CONTENT:
			String tmpHtml = "<html><head></head><body>content</body></html>";
			webView.loadDataWithBaseURL(null, tmpHtml.replace("content", android.text.Html.fromHtml(content)), "text/html", "utf-8", null);
			break;
		default:
			break;
		}
	}
	
	@Override
	public boolean isShowBackBtn() {
		return true;
	}
	
	@Override
	public void onResume() {
		super.onResume();
	}
	
	public void initShareSection() {
		if(shareInfo == null) {
			return;
		}
		
		//set visibility
		View shareSection = findViewById(R.id.share_section);
		shareSection.setVisibility(View.VISIBLE);
		//set btn event handler 
		btnShare = (Button)findViewById(R.id.news_detail_btn_share);
		btnShare.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				HelperFunc.trackEvent(NewsDetailActivity.this, "share", "btn_share_news_press", "btn_share_news_press");
				ShareActivity.shareInfo = shareInfo; 
				Intent intent = new Intent(NewsDetailActivity.this, ShareActivity.class);
				startActivity(intent);
			}
		});
	}
}
